package 买卖股票的最好时机2;

import java.util.Scanner;

public class Main {
    static int N = 100010;
    static int[] a = new int[N];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        int res = 0;
        for (int i = 1; i < n; i++) {
            if (a[i] - a[i - 1] > 0) {
                res += a[i] - a[i - 1];
            }
        }
        System.out.println(res);
    }
}
